package com.project.weimi.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.project.weimi.R;
import com.project.weimi.adapter.HomeFragmentViewPagerAdapter;
import com.project.weimi.adapter.TaoTaoAdapter;
import com.project.weimi.entity.response.TaoTaoEntity;
import com.project.weimi.view.CircleListView;
import com.project.weimi.view.HomeFragmentGridView;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint({ "InflateParams", "NewApi" })
public class HomeFragment extends BaseFragment {

	private HomeFragmentGridView gridView;
	// GridView图片和文字
	private int imgs[] = { R.drawable.icon_taotao, R.drawable.icon_gaogenxie, R.drawable.icon_yandai,
			R.drawable.icon_xiongzhao, R.drawable.icon_zhendongbang, R.drawable.icon_yanshi, R.drawable.icon_huodong,
			R.drawable.icon_mianfei };
	private String words[] = { "我要套套", "娘子莫走", "官人来玩", "变装尤戏", "花式调教", "宫廷延时", "最新活动", "免费体验" };

	private String[] taoTitle = { "我要套套", "变装尤戏", "娘子莫走", "官人来玩", "花式调教" };
	private String[] taoTvs = { "若隐又若现\n被网住的美人\nNET", "清纯女学生\n老师来调教\nSTUDENT" };
	private int[] taoImgs = { R.drawable.tao_item1, R.drawable.tao_item2 };

	private ViewPager vp;   
	
	// 轮播图下方的圆点
	private View view1, view2, view3, view4, view5, view6;
	// 存放轮播图片的集合
	private List<ImageView> list;
	// isMove手指是否触摸轮播图 isDes此Fragment是否执行onDestory
	private boolean isMove, isDestroy;
	// 轮播图2秒自动轮播
	public static final int TIME_CHANGE = 2000;
	private long desTime = 0; 
	// 横向滑动的列表
	private LinearLayout ll_gallery;

	private CircleListView taotao_listView;
	private List<TaoTaoEntity> taoList;
	private TaoTaoAdapter taoAdapter;

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			long nowTime = System.currentTimeMillis();
			if (!isDestroy) {
				Message mss = new Message();
				if (!isMove && (nowTime - desTime > TIME_CHANGE)) {
					int i = vp.getCurrentItem();
					vp.setCurrentItem(i + 1);
				}
				handler.sendMessageDelayed(mss, TIME_CHANGE);
			}
		};
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		// 解析布局文件
		View view = inflater.inflate(R.layout.fragment_home, null);

		initView(view);
		initGridView();
		initGalleryData();

		return view;
	}

	private void initView(View view) {
		gridView = (HomeFragmentGridView) view.findViewById(R.id.gridView);
		ll_gallery = (LinearLayout) view.findViewById(R.id.ll_gallery);

		view1 = (View) view.findViewById(R.id.view1);
		view2 = (View) view.findViewById(R.id.view2);
		view3 = (View) view.findViewById(R.id.view3);
		view4 = (View) view.findViewById(R.id.view4);
		view5 = (View) view.findViewById(R.id.view5);
		view6 = (View) view.findViewById(R.id.view6);
		vp = (ViewPager) view.findViewById(R.id.vp);
		vp.setLayoutParams(new FrameLayout.LayoutParams(getActivity().getResources().getDisplayMetrics().widthPixels,
				getActivity().getResources().getDisplayMetrics().widthPixels * 188 / 563));
		initViewPager();

		taotao_listView = (CircleListView) view.findViewById(R.id.taotao_listView);

		initTaoListView();
	}

	private void initTaoListView() {
		// TODO Auto-generated method stub
		taoList = new ArrayList<TaoTaoEntity>();
		List<ImageView> list = null;
		TaoTaoEntity taoEntity = null;
		for (int i = 0; i < 5; i++) {
			taoEntity = new TaoTaoEntity();
			taoEntity.setTao_title(taoTitle[i]);
			taoEntity.setTao_tv(taoTvs);
			taoEntity.setTao_iv(taoImgs);

			list = new ArrayList<ImageView>();
			for (int j = 0; j < 2; j++) {
				ImageView img1 = new ImageView(getActivity());
				img1.setImageResource(R.drawable.banner1);
				ImageView img2 = new ImageView(getActivity());
				img2.setImageResource(R.drawable.banner2);
				list.add(img1);
				list.add(img2);
			}
			taoEntity.setImageView(list);
			
			taoList.add(taoEntity);
		}

		taoAdapter = new TaoTaoAdapter(getActivity(), taoList);
		taotao_listView.setAdapter(taoAdapter);
	}

	private void initViewPager() {
		list = new ArrayList<ImageView>();
		for (int j = 0; j < 2; j++) {
			ImageView img1 = new ImageView(getActivity());
			img1.setImageResource(R.drawable.banner1);
			ImageView img2 = new ImageView(getActivity());
			img2.setImageResource(R.drawable.banner2);
			ImageView img3 = new ImageView(getActivity());
			img3.setImageResource(R.drawable.banner3);
			ImageView img4 = new ImageView(getActivity());
			img4.setImageResource(R.drawable.banner4);
			ImageView img5 = new ImageView(getActivity());
			img5.setImageResource(R.drawable.banner5);
			ImageView img6 = new ImageView(getActivity());
			img6.setImageResource(R.drawable.banner);
			list.add(img1);
			list.add(img2);
			list.add(img3);
			list.add(img4);
			list.add(img5);
			list.add(img6);
		}

		Log.i("aaa", "集合长度为======" + list.size());

		// ViewPager适配器
		HomeFragmentViewPagerAdapter adapter = new HomeFragmentViewPagerAdapter(getActivity(), list);
		vp.setAdapter(adapter);
		vp.setCurrentItem(100 * 1000);
		// ViewPager触摸事件
		vp.setOnTouchListener(touchListener);
		// Viewpager滑动监听
		vp.addOnPageChangeListener(pageChangeListener);
	}

	private OnTouchListener touchListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			int i = vp.getCurrentItem();
			int action = event.getAction();
			if (action == MotionEvent.ACTION_MOVE) {
				isMove = true;
				desTime = System.currentTimeMillis();
			}
			if (action == MotionEvent.ACTION_DOWN) {
			}
			if (action == MotionEvent.ACTION_UP) {
				if (!isMove) {
					String stri = i % list.size() + 1 + "条";
					// 点击事件在这里
					// Toast.makeText(getActivity(),
					// stri,Toast.LENGTH_SHORT).show();
				}
				isMove = false;
			}
			if (action == MotionEvent.ACTION_CANCEL) {
				// LogUtils.showTag("MainF", "cancel");
			}
			return false;
		}
	};

	private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {

		@Override
		public void onPageSelected(int arg0) {
			view1.setBackgroundResource(R.drawable.shape_trans_circle);
			view2.setBackgroundResource(R.drawable.shape_trans_circle);
			view3.setBackgroundResource(R.drawable.shape_trans_circle);
			view4.setBackgroundResource(R.drawable.shape_trans_circle);
			view5.setBackgroundResource(R.drawable.shape_trans_circle);
			view6.setBackgroundResource(R.drawable.shape_trans_circle);
			
			switch (arg0 % (list.size() / 2)) {
			case 0:
				view1.setBackgroundResource(R.drawable.shape_white_circle);
				break;
			case 1:
				view2.setBackgroundResource(R.drawable.shape_white_circle);
				break;
			case 2:
				view3.setBackgroundResource(R.drawable.shape_white_circle);
				break;
			case 3:
				view4.setBackgroundResource(R.drawable.shape_white_circle);
				break;
			case 4:
				view5.setBackgroundResource(R.drawable.shape_white_circle);
				break;
			case 5:
				view6.setBackgroundResource(R.drawable.shape_white_circle);
				break;
			default:
				break;
			}
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	};

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if (hidden) {
			// 隐藏后 让轮播图取消轮播
			isDestroy = true;
		} else {
			isDestroy = false;
			handler.removeCallbacksAndMessages(null);
			handler.sendMessageDelayed(new Message(), TIME_CHANGE);
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		isDestroy = true;
		handler.removeCallbacksAndMessages(null);
	}

	@Override
	public void onResume() {
		super.onResume();
		isDestroy = false;
		handler.removeCallbacksAndMessages(null);
		handler.sendMessageDelayed(new Message(), TIME_CHANGE);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		isDestroy = true;
	}

	private void initGridView() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = null;
		for (int i = 0; i < imgs.length; i++) {
			map = new HashMap<String, Object>();
			map.put("imgs", imgs[i]);
			map.put("words", words[i]);
			list.add(map);
		}
		// GridView适配器
		gridView.setAdapter(new SimpleAdapter(getActivity(), list, R.layout.gridview_item,
				new String[] { "imgs", "words" }, new int[] { R.id.img, R.id.tv }));
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				
				
				
			}
		});
	}

	private void initGalleryData() {
		int galleryImgs[] = { R.drawable.a, R.drawable.e, R.drawable.a, R.drawable.e, R.drawable.e, R.drawable.e };
		String galleryTvs[] = { "磨人的小恶魔", "来自大自然的问候", "热辣的美女诱惑", "仙剑奇侠传", "恋曲1990", "别问我是谁" };

		for (int i = 0; i < galleryImgs.length; i++) {
			View view = LayoutInflater.from(getActivity()).inflate(R.layout.homefragment_gallery_item, ll_gallery,
					false);
			ImageView img_gallery = (ImageView) view.findViewById(R.id.img_gallery);

			if (i != 0) {
				LayoutParams params = (LayoutParams) view.getLayoutParams();
				params.setMargins(10, 0, 0, 0);
				view.setLayoutParams(params);
			}

			img_gallery.setImageResource(galleryImgs[i]);
			TextView tv_gallery = (TextView) view.findViewById(R.id.tv_gallery);
			tv_gallery.setText(galleryTvs[i]);
			final int o = i;
			view.setOnClickListener(new OnClickListener() {
				int j = o;

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Toast.makeText(getActivity(), "" + j, 0).show();
				}
			});

			ll_gallery.addView(view);
		}
	}

}
